﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Owl.Contracts;
using System.Data.Entity;
using System.Diagnostics;
using Owl.Questionnarie.Entities;

namespace Owl.Questionnarie.DAL
{
    public class QuestionnarieDAL : ICrud<Questionnaries, Guid>
    {

        /// <summary>
        /// Recupera um único questionário pelo seu ID.
        /// </summary>
        /// <param name="key">chave primária</param>
        /// <returns>Questionnaries</returns>
        public Questionnaries GetById(Guid key)
        {
            Trace.WriteLine("QuestionnairesDAL", "Iniciando GetById");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    Questionnaries q = (from questionnarie in context.Questionnaries
                                        where questionnarie.QuestionnarieId == key
                                        select questionnarie).FirstOrDefault();

                    Trace.WriteLine("QuestionnairesDAL", "Retornando objeto");
                    return q;
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionnairesDAL", ex.Message);
                throw;
            }
        } //Fim do GetById

        /// <summary>
        /// Recupera todos os questionários
        /// </summary>
        /// <returns>List-Questionnaries</returns>
        public List<Questionnaries> GetAll()
        {
            Trace.WriteLine("QuestionnairesDAL", "Iniciando GetAll");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    List<Questionnaries> q = (from questionnarie in context.Questionnaries
                                              select questionnarie).ToList();

                    Trace.WriteLine("QuestionnairesDAL", "Retornando objetos");
                    return q;
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionnairesDAL", ex.Message);
                throw;
            }
        } //Fim do GetALL

        /// <summary>
        /// Recupera uma lista de questionários já respondidos por um usuário
        /// </summary>
        /// <param name="userEmail">email do usuário</param>
        /// <returns>List-Questionnaries</returns>
        public List<Questionnaries> GetAllByUser(string userEmail)
        {
            Trace.WriteLine("QuestionnairesDAL", "Iniciando GetAllByUser");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {
                    List<Questionnaries> listQuestionnarie = (from q in context.User_Questionnaries
                                                              where q.UserData.UserEmail == userEmail
                                                              select q.Questionnarie).ToList();

                    Trace.WriteLine("QuestionnairesDAL", "Retornando objeto");
                    return listQuestionnarie;
                }


            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionnairesDAL", ex.Message);
                throw;
            }
        } //Fim do GetAllByUser

        /// <summary>
        /// Recupera uma lista de questionários por descrição
        /// </summary>
        /// <param name="text">Sumário</param>
        /// <returns>List-Questionnaries</returns>
        public List<Questionnaries> GetByString(string text)
        {
            Trace.WriteLine("QuestionnairesDAL", "Iniciando GetByString");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    List<Questionnaries> q = (from questionnarie in context.Questionnaries
                                              where questionnarie.Summary.Contains(text)
                                              select questionnarie).ToList();
                    Trace.WriteLine("QuestionnairesDAL", "Retornando objeto");
                    return q;
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionnairesDAL", ex.Message);
                throw;
            }
        } //Fim do GetByString

        /// <summary>
        /// Insere questionários novos
        /// </summary>
        /// <param name="entity">Questionário e seus filhos</param>
        /// <returns>Questionnaries</returns>
        public Questionnaries Insert(Questionnaries entity)
        {
            Trace.WriteLine("QuestionnairesDAL", "Iniciando insert");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    entity.QuestionnarieId = Guid.NewGuid();
                    context.AddToQuestionnaries(entity);
                    context.SaveChanges();
                    Trace.WriteLine("QuestionnairesDAL", "Inserção realizada");

                    Trace.WriteLine("QuestionnairesDAL", "Retornando objeto");
                    return entity = (from questionnarie in context.Questionnaries
                                     where questionnarie.QuestionnarieId == entity.QuestionnarieId
                                     select questionnarie).FirstOrDefault();
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionnairesDAL", ex.Message);
                throw;
            }
        } //Fim do Insert

        /// <summary>
        /// Atualiza os dados do questionário
        /// </summary>
        /// <param name="entity">Questionário e seus filhos</param>
        public void Update(Questionnaries entity)
        {
            Trace.WriteLine("QuestionnairesDAL", "Iniciando update");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    Questionnaries q = (from questionnarie in context.Questionnaries
                                        where questionnarie.QuestionnarieId == entity.QuestionnarieId
                                        select questionnarie).FirstOrDefault();

                    q.Avaiable = entity.Avaiable;
                    q.Summary = entity.Summary;
                    context.SaveChanges();
                    Trace.WriteLine("QuestionnairesDAL", "Alterações realizadas");
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionnairesDAL", ex.Message);
                throw;
            }
        } //Fim do Update

        /// <summary>
        /// Deleta o questionário pela sua chave.
        /// </summary>
        /// <param name="key">Chave primária</param>
        public void Delete(Guid key)
        {
            Trace.WriteLine("QuestionnairesDAL", "Iniciando Delete");
            try
            {
                using (QuestionnarieEntities context = new QuestionnarieEntities())
                {

                    Questionnaries q = (from questionnarie in context.Questionnaries
                                        where questionnarie.QuestionnarieId == key
                                        select questionnarie).FirstOrDefault();

                    context.DeleteObject(q);
                    context.SaveChanges();
                    Trace.WriteLine("QuestionnairesDAL", "Deleção realizada");
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine("QuestionnairesDAL", ex.Message);
                throw;
            }
        } //Fim do Delete



    } //Fim da Classe
}
